home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / CHESS / PATHPLAN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  1.4 KB  |  86 lines

  1. /*
  2.  * pathplan.c - part of the chess demo in the glut distribution.
  3.  *
  4.  * (C) Henk Kok (kok@wins.uva.nl)
  5.  *
  6.  * This file can be freely copied, changed, redistributed, etc. as long as
  7.  * this copyright notice stays intact.
  8.  */
  9.  
  10. #include "chess.h"
  11.  
  12. extern int board[10][10];
  13.  
  14. int path[10][10];
  15. int hops[10][10];
  16.  
  17. int steps;
  18. int cur_hops;
  19.  
  20. void init_board(void)
  21. {
  22.     int i,j;
  23.     for (i=0;i<10;i++)
  24.     {
  25.     for(j=0;j<10;j++)
  26.     {
  27.         hops[i][j] = 0;
  28.         path[i][j] = (board[i][j]?-1:0);
  29.     }
  30.     }
  31. }
  32.  
  33. void test_exit(int i, int j, int dir)
  34. {
  35.     if (i<0 || i>9 || j<0 || j>9)
  36.     return;
  37.     if (path[i][j])
  38.     return;
  39.     steps ++;
  40.     path[i][j] = dir;
  41.     hops[i][j] = cur_hops + 1;
  42. }
  43.  
  44. int solve_path(int x1, int y1, int x2, int y2)
  45. {
  46.     int i,j;
  47.     init_board();
  48.     path[x2][y2] = 9;
  49.     hops[x2][y2] = 1;
  50.     path[x1][y1] = 0;
  51.     cur_hops = 1;
  52.     for (;;)
  53.     {
  54.     steps = 0;
  55.     for (i=0;i<10;i++)
  56.     {
  57.         for (j=0;j<10;j++)
  58.         {
  59.         if (hops[i][j] != cur_hops)
  60.             continue;
  61.         test_exit(i, j-1, SOUTH);
  62.         test_exit(i, j+1, NORTH);
  63.         test_exit(i-1, j, EAST);
  64.         test_exit(i+1, j, WEST);
  65.         }
  66.     }
  67.     for (i=0;i<10;i++)
  68.     {
  69.         for (j=0;j<10;j++)
  70.         {
  71.         if (hops[i][j] != cur_hops)
  72.             continue;
  73.         test_exit(i-1, j-1, SOUTHEAST);
  74.         test_exit(i+1, j-1, SOUTHWEST);
  75.         test_exit(i-1, j+1, NORTHEAST);
  76.         test_exit(i+1, j+1, NORTHWEST);
  77.         }
  78.     }
  79.     cur_hops++;
  80.     if (path[x1][y1])
  81.         return 1;
  82.     if (steps == 0)
  83.         return 0;
  84.     }
  85. }
  86.